// 这里将action对象的type属性值写成了常量，便于reducer引用，减少了出错的概率。
export const INCREMENT_COUNTER = 'INCREMENT_COUNTER';
export const DECREMENT_COUNTER = 'DECREMENT_COUNTER';

console.log('[actions/index.js] ->加载');

export function increment() {
    console.log('[actions/index.js] ->increment');
    return {
        type: INCREMENT_COUNTER,
    };
}

export function decrement() {

    console.log('[actions/index.js] ->decrement');
    return {
        type: DECREMENT_COUNTER,
    };
}

export function incrementIfOdd() {
    console.log('[actions/index.js] ->incrementIfOdd');

    //redux-thunk中间支持 返回函数
    return (dispatch, getState) => {
        const { counter } = getState();

        if (counter % 2 === 0) {
            return;
        }

        dispatch(increment());
    };
}

export function incrementAsync(delay = 1000) {
    console.log('[actions/index.js] ->incrementAsync');

    //redux-thunk中间支持 返回函数
    return dispatch => {
        setTimeout(() => {
            dispatch(increment());
        }, delay);
    };
}
